﻿using Microsoft.Xna.Framework;
using Terraria;
using Terraria.ModLoader;
using WorseGame.Common.Configs;

namespace WorseGame.Common.GlobalProjectiles
{
    public class WorseProjectile : GlobalProjectile
    {
        public override bool InstancePerEntity => true;
        protected override bool CloneNewInstances => true;
        public override void ModifyHitPlayer(Projectile projectile, Player target, ref Player.HurtModifiers modifiers)
        {
            if (ProjectileConfig.Instance.ProjectileCritChance)
            {
                if (!projectile.friendly && projectile.hostile)
                {
                    float critMultiplier = Main.rand.NextFloat(1.1f, 1.5f);
                    modifiers.FinalDamage *= critMultiplier;
                }
            }
        }
        public override void ModifyHitNPC(Projectile projectile, NPC target, ref NPC.HitModifiers modifiers)
        {
            if (ProjectileConfig.Instance.ProjectileCritChance)
            {
                if (!projectile.friendly && projectile.hostile)
                {
                    float critMultiplier = Main.rand.NextFloat(1.1f, 1.5f);
                    modifiers.FinalDamage *= critMultiplier;
                }
            }
        }
        private static bool ShouldNotDraw(int projectileId)
        {
            if (projectileId % 2 == 0)
            {
                return true;
            }
            if (projectileId < 2)
            {
                return false;
            }
            for (int i = 2; i * i <= projectileId; i++)
            {
                if (projectileId % i == 0)
                {
                    return false;
                }
            }
            return true;
        }
        public override bool PreDraw(Projectile projectile, ref Color lightColor)
        {
            if (ProjectileConfig.Instance.ProjectileInvisibility)
            {
                if (ShouldNotDraw(projectile.type))
                {
                    return false;
                }
            }
            return base.PreDraw(projectile, ref lightColor);
        }
    }
}
